<!--@ 首页 -->
<template>
	<view class="bg-white" style="min-height: 100vh;">
		<load-flex :loading="loading"></load-flex>
		<u-notify ref="uNotify" />
		<view v-if="!showWu">
			<u-sticky>
				<view class="bg-white padding-tb">
					<u-steps activeColor="#013E8E" :current="courrentIndex">
						<u-steps-item :title="item" activeColor="#013E8E" v-for="(item,index) in dataClueStage"
							:key="index" />
					</u-steps>
				</view>
			</u-sticky>
			<view>
				<userNameFrom ref="userNameFrom" :ageSync="ageSync" :dataPrimitive="dataPrimitive"
					v-show="courrentIndex ==0" @onUserName="handeUserName" />
				<indexFrom ref="indexFrom" :ageSync="ageSync" :dataPrimitive="dataPrimitive" v-show="courrentIndex ==1"
					@onIndex="handeIndex" @onLast="handeLast" />
				<loanFrom ref="loanFrom" :ageSync="ageSync" :dataPrimitive="dataPrimitive" v-show="courrentIndex ==2"
					@onLoan="handeLoan" @onLast="handeLast" />
			</view>
		</view>
		<u-modal :show="showWu" :showCancelButton="false" :showConfirmButton="false">
			<view style="height: 200rpx;" class="flex justify-center align-center">
				<text class="text-lg text-orange">订单已流转到其他部门，请开辟新的订单</text>
			</view>
		</u-modal>
	</view>
</template>
<script>
	import loadFlex from '@/components/loadFlex/loadFlex.vue' //加载框
	import userNameFrom from '@/components/assemblyModa/userNameFrom.vue' //用户信息
	import indexFrom from '@/components/assemblyModa/indexFrom.vue' //信用卡信息
	import loanFrom from '@/components/assemblyModa/loanFrom.vue' //贷款信息
	import myApi from '@/utils/app.js' //调用接口使
	export default {
		name: 'assembly',
		components: {
			loadFlex,
			userNameFrom,
			indexFrom,
			loanFrom
		},
		data() {
			return {
				dataSource: [],
				loading: false,
				courrentIndex: 0,
				model: {},
				ageSync: {},
				dataPrimitive: {},
				showWu: false,
				dataClueStage: ['个人信息', '信用卡信息', '贷款信息'],
			}
		},
		onLoad() {
			// this.loading = true
			this.showWu = false
			const local = window.location.href
			this.model = {}
				// const local = 'http://szrswms.com/#/pages/assembly/assembly?id=1870848765998751746&clueId=1870848444413075457&orderNo=1870848766014263296'
			this.model.mainId = local.match(/id=([^?&]+)/)[1];
			if (local.match(/clueId=([^?&]+)/)[1]) this.model.clueId = local.match(/clueId=([^?&]+)/)[1];
			if (local.match(/orderNo=([^?&]+)/)[1]) this.model.orderNo = local.match(/orderNo=([^?&]+)/)[1];
			this.loadData2()
		},
		onHide: function() {
			let mode = {
				data: [],
				mainId: this.model.mainId,
				clueId: this.model.clueId,
				orderNo: this.model.orderNo,
			}
			if (this.courrentIndex == 0) mode.data[0] = this.$refs.userNameFrom.model;
			if (this.courrentIndex == 1) {
				mode.data[0] = this.dataSource[0]
				mode.data[1] = this.$refs.indexFrom.dataSource
			}
			if (this.courrentIndex == 2) {
				mode.data[0] = this.dataSource[0]
				mode.data[1] = this.dataSource[1]
				mode.data[2] = this.$refs.loanFrom.dataSource
			}
			uni.setStorageSync(`${this.model.clueId}${this.model.mainId}`, mode);
		},
		onUnload: function() {
			let mode = {
				data: [],
				mainId: this.model.mainId,
				clueId: this.model.clueId,
				orderNo: this.model.orderNo,
			}
			if (this.courrentIndex == 0) mode.data[0] = this.$refs.userNameFrom.model;
			if (this.courrentIndex == 1) {
				mode.data[0] = this.dataSource[0]
				mode.data[1] = this.$refs.indexFrom.dataSource
			}
			if (this.courrentIndex == 2) {
				mode.data[0] = this.dataSource[0]
				mode.data[1] = this.dataSource[1]
				mode.data[2] = this.$refs.loanFrom.dataSource
			}
			uni.setStorageSync(this.model.clueId, mode);
		},
		methods: {
			handeUserName(data) { // 用户信息返回的数据
				this.dataSource[0] = data
				this.courrentIndex = 1
				this.$refs.indexFrom.onInfrom(data)
			},
			handeIndex(data) { // 贷款信息 数据
				this.dataSource[1] = data
				this.$refs.loanFrom.onInfrom(data[0])
				this.courrentIndex = 2
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			handeLoan(data) {
				this.dataSource[2] = data
				this.loadData()
			},
			async loadData2() {
				this.loading = true
				try {
					const res = await this.$request({
						url: myApi.judgeShareExist,
						method: 'get',
						data: {
							orderNo: this.model.orderNo
						},
					});
					this.loading = false
					if (res.code == 200) {
						let mode = uni.getStorageSync(`${this.model.clueId}${this.model.mainId}`)
						if (mode && mode.data && mode.data[0]) {
							this.courrentIndex = mode.data.length - 1
							this.dataSource = mode.data
							this.ageSync = mode
							console.log(this.ageSync)
						}
					}
					if (res.code == 201) {
						this.dataPrimitive = res.result
						this.courrentIndex = 1
						return
					}
					if (res.code == 500) return this.showWu = true

				} catch (e) {
					// 失败执行

				} finally {
					// 销毁执行
					this.loading = false
				}
			},

			async loadData() {
				this.loading = true
				let dataObj = {}
				if (this.dataPrimitive.customerInfo && this.dataPrimitive.customerInfo.id) {
					dataObj = {
						inviter: this.dataPrimitive.inviter,
						mainId: this.dataPrimitive.mainId,
						orderNo: this.model.orderNo,
						phone: this.dataPrimitive.phone,
						clueId: this.model.clueId,
						bmsCustomerInfo: this.dataPrimitive.customerInfo,
						formCreditCardInfoList: this.dataSource[1].length > 0 ? this.dataSource[1].filter(item => !
							item.id) : undefined,
						bmsLoanDetailVto: {
							detailList: this.dataSource[2] && this.dataSource[2].detailList && this.dataSource[2]
								.detailList ? this.dataSource[2].detailList.filter(item => !item.id) : undefined,
							inviter: this.dataPrimitive.inviter,
							mainId: this.dataPrimitive.mainId,
							orderNo: this.model.orderNo,
							phone: this.dataPrimitive.phone,
							clueId: this.model.clueId,
						}
					}
				} else {
					dataObj = {
						...this.model,
						bmsCustomerInfo: this.dataSource[0],
						bmsLoanDetailVto: {
							detailList: this.dataSource[2] && this.dataSource[2].detailList && this.dataSource[2]
								.detailList.length > 0 ? this.dataSource[2].detailList : undefined,
							inviter: this.model.mainId,
							phone: this.dataSource[0].phone,
							mainId: this.model.mainId,
							orderNo: this.model.orderNo,
							clueId: this.model.clueId
						},
						formCreditCardInfoList: this.dataSource[1].length > 0 ? this.dataSource[1] : undefined,
						inviter: this.model.mainId,
						phone: this.dataSource[0].phone,
					}
				}
				console.log(dataObj)
				try {
					const res = await this.$request({
						url: this.dataPrimitive.customerInfo && this.dataPrimitive.customerInfo.id ? myApi
							.makeUpShareForm : myApi.shareForm,
						method: 'post',
						data: dataObj,
					});
					console.log(res)
					if (res.code == 200) {
						this.onNotify({
							message: '提交成功'
						})
						setTimeout(() => (
							uni.reLaunch({
								url: '/pages/successful/successful'
							})), 400);
					} else {
						this.onNotify({
							message: res.message,
							type: 'error'
						})
					}
				} catch (e) {
					// 失败执行
					this.onNotify({
						message: '操作失败',
						type: 'error'
					})
				} finally {
					// 销毁执行
					this.loading = false
				}
			},
			handeLast(index) { // 返回上一步
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.courrentIndex = index
			},
			onNotify(round) {
				this.$refs.uNotify.show({
					type: round.type ? round.type : 'success',
					message: round.message ? round.message : '删除成功',
					top: 50
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		/* 以下属性根据实际需要进行调整 */
		-webkit-backdrop-filter: none;
		backdrop-filter: none;
	}
</style>